package br.com.edumobi.fragments;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;

import java.util.List;

import br.com.edumobi.R;
import br.com.edumobi.adapters.ListaClassificacoesAdapter;
import br.com.edumobi.dados.modelos.Aluno;
import br.com.edumobi.dados.modelos.Classificacao;

public class DashboardFragment extends GenericoFragment {

    private ListView listViewClassificacoesFragment;
    private ProgressBar progressBarDashboardFragment;

    private AcoesDashboardFragment activity;
    private DashboardTask task;

    private List<Classificacao> classificacoesSeperiores;

    private AdapterView.OnItemClickListener clickItemListViewClassificacoesFragment = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Classificacao classificacao = classificacoesSeperiores.get(position);

            if (!activity.obterClassificacoesSuperiores(classificacao).isEmpty()) {
                activity.abrirClassificacao(classificacao);
            } else {
                activity.exibirTexto(R.string.sem_mais_conteudo);
            }
        }
    };

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        activity = (AcoesDashboardFragment) getActivity();

        if (task == null || task.getStatus().equals(AsyncTask.Status.FINISHED)) {
            task = new DashboardTask();
            task.execute();
        }

        carregarListViewClassificacoes();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dashboard, container, false);

        listViewClassificacoesFragment = (ListView) view.findViewById(R.id.listViewClassificacoesFragment);
        progressBarDashboardFragment = (ProgressBar) view.findViewById(R.id.progressBarDashboardFragment);

        listViewClassificacoesFragment.setOnItemClickListener(clickItemListViewClassificacoesFragment);

        return view;
    }

    @Override
    public void exibirComponentes() {
        listViewClassificacoesFragment.setVisibility(View.VISIBLE);
        progressBarDashboardFragment.setVisibility(View.VISIBLE);
    }

    @Override
    public void esconderComponentes() {
        listViewClassificacoesFragment.setVisibility(View.GONE);
        progressBarDashboardFragment.setVisibility(View.GONE);
    }

    private void carregarListViewClassificacoes() {
        classificacoesSeperiores = activity.obterClassificacoesSuperiores(null);
        listViewClassificacoesFragment.setAdapter(new ListaClassificacoesAdapter(classificacoesSeperiores));
    }

    private class DashboardTask extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected void onPreExecute() {
            esconderComponentes();
            progressBarDashboardFragment.setVisibility(View.VISIBLE);
            activity.falarTexto(R.string.aguarde);
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            return activity.atualizarConteudoLocal();
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            exibirComponentes();
            progressBarDashboardFragment.setVisibility(View.GONE);
            if (aBoolean) {
                carregarListViewClassificacoes();
            } else {
                activity.exibirTexto(R.string.excecao_conteudo_local_nao_atualizado);
            }
        }
    }

    public interface AcoesDashboardFragment extends AcoesGenericoFragment {
        Aluno obterAluno();
        List<Classificacao> obterClassificacoesSuperiores(Classificacao classificacaoSuperior);
        void abrirClassificacao(Classificacao classificacao);
        Boolean atualizarConteudoLocal();
    }
}
